package online.heycm.platform.nacos;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * 配置监听抽象类，继承此类，实现 receiveConf 方法，注册为 Spring Bean 即可实现配置监听
 *
 * @author hey
 * @version 1.0
 * @date 2024/1/19 10:12
 */
public abstract class AbstractNacosConfListener {

    private final String dataId;

    private final String group;

    @Autowired
    private NacosConfListener nacosConfListener;

    protected AbstractNacosConfListener(String dataId) {
        this.dataId = dataId;
        this.group = "DEFAULT_GROUP";
        nacosConfListener.addListener(dataId, group, this::receiveConf);
    }

    protected AbstractNacosConfListener(String dataId, String group) {
        this.dataId = dataId;
        this.group = group;
        nacosConfListener.addListener(dataId, group, this::receiveConf);
    }

    /**
     * 接收配置信息
     *
     * @param config 配置信息
     */
    protected abstract void receiveConf(String config);

    public String getDataId() {
        return dataId;
    }

    public String getGroup() {
        return group;
    }
}
